script_path=$(realpath $(dirname $0))

op_path=$1
python_script_path=$2
use_profiler=0
if [ $# -ge 1 ]; then
    if [ $1 = "help" ]; then
        echo "参数1：算子目录"
        echo "参数2：脚本目录"
        echo "参数3：是否采集性能数据([0]|1)"
        echo "参数4：是否重新编译([0]|1)"
        exit
    fi
fi

if [ $# -ge 3 ]; then
    if [ $3 = "1" ]; then
        use_profiler=1
    fi
fi

if [ $# -ge 4 ]; then
    if [ $4 = "1" ]; then
        bash scripts/op_build_cross_310b.sh $1
    fi
fi

. $script_path/sync.sh

echo 开始执行
ssh -p ${REMOTE_PORT} $REMOTE_HOST << EOF
if [ -f ~/Ascend/ascend-toolkit/set_env.sh ]; then
    source ~/Ascend/ascend-toolkit/set_env.sh
elif [ -f /usr/local/Ascend/ascend-toolkit/set_env.sh ]; then
    source /usr/local/Ascend/ascend-toolkit/set_env.sh
fi
cd $REMOTE_DIR

echo 安装算子
bash `ls $op_path/build_out/*.run`

echo 删除log
rm /var/log/npu/slog -rf

cd $REMOTE_DIR
if [ -f /usr/bin/python3 ]; then
    if [ $use_profiler = "1" ]; then
        rm -rf ./profiles
        msprof --application="/usr/bin/python3 $python_script_path" --output=./profiles
    else
        /usr/bin/python3 $python_script_path
    fi
elif [ -f /home/ma-user/anaconda3/envs/MindSpore/bin/python ]; then
    /home/ma-user/anaconda3/envs/MindSpore/bin/python $python_script_path
fi
EOF

if [ $use_profiler = "1" ]; then
    echo 开始拷贝 $REMOTE_HOST:$REMOTE_DIR/profiles
    rm  -rf ./profiles 
    scp -q -P ${REMOTE_PORT} -r $REMOTE_HOST:$REMOTE_DIR/profiles ./
fi